package com.intro.action;

import java.util.Date;

import com.intro.database.dao.NewsDao;
import com.intro.database.entity.Image;
import com.intro.database.entity.News;
import com.intro.util.TimeUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.GETSTATIC;

import javax.persistence.Entity;

import org.apache.struts2.ServletActionContext;

@Entity
public class AddNewsAction extends ActionSupport {
	private String content;
	private String title;
	private String image_src;
	private String date_str;
	private int type;
	public String execute() throws Exception {
		
		//this.get
		//System.out.println(image_src);
		News news = new News();
		news.setContent(content);
		news.setTitle(title);
		if(type == News.TYPE_NEWS) {
			String[] imagepaths = image_src.split(":");
			for(int i = 0;i<imagepaths.length;i++) {
				Image image = new Image();
				image.setImageSrc(imagepaths[i]);
				news.getImages().add(image);
			}
			news.setImagePath("");
		} else {
			news.setImagePath(image_src);
		}
		news.setDate(new Date());
		news.setDate(TimeUtil.transString2Date(date_str));
		news.setType(type);
		NewsDao dao = new NewsDao();
		String result = ERROR;
		if(dao.save(news)) {
			ServletActionContext.getRequest().setAttribute("type", news.getType());
			result = SUCCESS;
		}
		return result;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getImage_src() {
		return image_src;
	}

	public void setImage_src(String imageSrc) {
		image_src = imageSrc;
	}

	public void setDate_str(String date_str) {
		this.date_str = date_str;
	}
	
	public void setType(int type) {
		this.type = type;
	}
	
}
